const departmentModel = require("./department");
const permissionModel = require("./permissions");
const roleModel = require("./role");
const userModel = require("./user");
const bcrypt = require("bcrypt");

const dotenv = require("dotenv");
dotenv.config({ path: `.env` });

// 创建部门
async function createDepartment() {
	const root_dep = await departmentModel.create({ name: "石家庄甜狗网络科技", key: "root", sort: -999 });
	const tech_dep = await departmentModel.create({ name: "技术部", key: "tech", pid: root_dep._id, sort: 10 });
	const personnel_dep = await departmentModel.create({ name: "人事部", key: "personnel", pid: root_dep._id, sort: 20 });

	return { root_dep, tech_dep, personnel_dep };
}

// 创建权限
async function createPermission() {
	// 创建首页菜单
	const createHomeRet = await permissionModel.create({
		permission_name: "首页",
		name: "home",
		key: "home",
		path: "/home",
		component: "/home/index",
		type: "menu",
		icon: "HomeFilled",
		sort: -1,
	});

	// 创建系统管理
	const createSystemRet = await permissionModel.create({
		permission_name: "系统管理",
		name: "system",
		type: "catalogue",
		icon: "Tools",
		sort: 999,
		path: "/system",
	});
	const system_1 = await permissionModel.create({
		permission_name: "用户管理",
		name: "system_user",
		key: "system_menu:user",
		type: "menu",
		icon: "UserFilled",
		sort: 10,
		path: "/system/user",
		component: "/system/user/index",
		pid: createSystemRet._id,
	});
	const system_2 = await permissionModel.create({
		permission_name: "角色管理",
		name: "system_role",
		key: "system_menu:role",
		type: "menu",
		icon: "Avatar",
		sort: 20,
		path: "/system/role",
		component: "/system/role/index",
		pid: createSystemRet._id,
	});
	const system_3 = await permissionModel.create({
		permission_name: "部门管理",
		name: "system_department",
		key: "system_menu:department",
		type: "menu",
		icon: "HelpFilled",
		sort: 30,
		path: "/system/department",
		component: "/system/department/index",
		pid: createSystemRet._id,
	});
	const system_4 = await permissionModel.create({
		permission_name: "权限管理",
		name: "system_permission",
		key: "system_menu:permission",
		type: "menu",
		icon: "Operation",
		sort: 40,
		path: "/system/permission",
		component: "/system/permission/index",
		pid: createSystemRet._id,
	});

	return {
		createHomeRet,

		createSystemRet,
		system_1,
		system_2,
		system_3,
		system_4,
	};
}

// 创建角色
async function createRole({ createHomeRet, createSystemRet, system_1, system_3 }) {
	// 人事角色
	const personnel_role = await roleModel.create({
		name: "人事",
		key: "personnel",
		permissions: [createHomeRet._id, createSystemRet._id, system_1._id, system_3._id],
	});

	return { personnel_role };
}

// 创建用户
async function createUser({ root_dep, personnel_role, personnel_dep }) {
	const pwd = bcrypt.hashSync("123456", 10);

	// 创建超级管理员
	await userModel.create({
		isSuper: true,
		nickname: "超级管理员",
		username: "admin",
		password: pwd,
		department: root_dep._id,
		role: [],
		phone: "18888888888",
	});
	// 创建人事
	await userModel.create({
		nickname: "张三",
		username: "zhangSan",
		password: pwd,
		department: personnel_dep._id,
		role: [personnel_role._id],
		phone: "18888888888",
	});
}

(async () => {
	// 清除数据库
	await userModel.deleteMany({});
	await roleModel.deleteMany({});
	await permissionModel.deleteMany({});
	await departmentModel.deleteMany({});

	const depRes = await createDepartment();
	const premissionsRes = await createPermission();
	const roleRes = await createRole(premissionsRes);
	await createUser({ ...roleRes, ...depRes });

	console.log("初始化数据库成功");
	process.exit();
})();
